package logic

import (
	"beike/cache"
	"beike/models"
	"context"
	"encoding/json"
	"time"

	"beike/houserpc/houserpc"
	"beike/houserpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type HouseShowLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHouseShowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HouseShowLogic {
	return &HouseShowLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HouseShowLogic) HouseShow(in *houserpc.HouseShowRequest) (*houserpc.HouseShowResponse, error) {
	// todo: add your logic here and delete this line

	var house models.House

	result, _ := cache.Rdb.Get(l.ctx, "houseShow").Result()

	if result == "" {
		models.DB.Debug().Where("id = ?", in.HouseId).Find(&house)
		json.Marshal(house)
		cache.Rdb.Set(l.ctx, "houseShow", house, time.Minute*15)
	} else {
		json.Unmarshal([]byte(result), &house)
	}

	return &houserpc.HouseShowResponse{
		UserId:  house.UserId,
		Title:   house.Title,
		Intro:   house.Intro,
		Price:   float32(house.Price),
		Address: house.Address,
		Types:   house.Types,
		Area:    float32(house.Area),
	}, nil
}
